package com.course.service;

import com.api.entity.Course;
import com.course.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/*
    @ClassName CourseService
    @Description TODO
    @Date 2021/9/2、15:18
*/
@Service
public class CourseService {

    @Autowired
    private CourseMapper mapper;

    @Resource
    private RedisTemplate<Object,Object> redisTemplate;

    public List<Course> getAllCourse(){

//        RedisSerializer rs = new StringRedisSerializer();
//        redisTemplate.setKeySerializer(rs);
//
//        List<Course> list = (List<Course>)redisTemplate.opsForValue().get("list");
//        if(list == null){
//            //排队，让第一个人进，走一遍流程（后面的人就会走缓存了）
//            synchronized (this){
//                list = (List<Course>)redisTemplate.opsForValue().get("list");
//                if(list == null){
//                    //去数据库
//                    System.out.println("====MySql数据库====");
//                    list = mapper.getAllCourse();
//                    // 把从数据库查询的集合放在redis内存中
//                    redisTemplate.opsForValue().set("list", list,60000*30, TimeUnit.SECONDS);
//                }
//            }
//        }
        return mapper.getAllCourse();
    }
    public List<Course> getCourseByPurchase(List<Integer> ids){
        return mapper.getCourseByPurchase(ids);
    }
}
